const app = getApp()
// pages/filter/filter.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fuelList: [],
    dragweightList: [],
    weightList: [],
    horsepowerList: [],

    fuelSel: [],
    weightSel: [],
    dragweightSel: [],
    horsepowerSel: [],
    ishotSel: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getFuelList();
    this.getDragweightList();
    this.getWeightList();
    this.getHorsepowerList();
    this.initIshotSel();
  },

  getFuelList: function () {
    let that = this;
    var data = {};
    app.get('Truck/gettruckfuel', data, function (res) {
      if (res.code == 200 && res.data.length > 0) {
        let list = that.data.fuelList
        var fuelSelData = [];
        for (let i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
          fuelSelData.push(0);
        }
        that.setData({
          fuelList: list,
          fuelSel: fuelSelData
        })
      }
    })
  },
  getDragweightList: function () {
    let that = this;
    var data = {};
    app.get('Truck/gettruckdragweight', data, function (res) {
      if (res.code == 200 && res.data.length > 0) {
        let list = that.data.dragweightList
        var dragweightSelData = [];
        for (let i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
          dragweightSelData.push(0);
        }
        that.setData({
          dragweightList: list,
          dragweightSel: dragweightSelData
        })
      }
    })
  },
  getWeightList: function () {
    let that = this;
    var data = {};
    app.get('Truck/gettruckweight', data, function (res) {
      if (res.code == 200 && res.data.length > 0) {
        let list = that.data.weightList
        var weightSelData = [];
        for (let i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
          weightSelData.push(0);
        }
        that.setData({
          weightList: list,
          weightSel: weightSelData
        })
      }
    })
  },
  getHorsepowerList: function () {
    let that = this;
    var data = {};
    app.get('Truck/gettruckhorsepower', data, function (res) {
      if (res.code == 200 && res.data.length > 0) {
        let list = that.data.horsepowerList
        var horsepowerSelData = [];
        for (let i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
          horsepowerSelData.push(0);
        }
        that.setData({
          horsepowerList: list,
          horsepowerSel: horsepowerSelData
        })
      }
    })
  },
  initIshotSel() {
    var ishotSelData = [];
    ishotSelData.push(0);
    ishotSelData.push(0);
    this.setData({ishotSel: ishotSelData});
  },

  selectFuel: function(e) {
    let idx = e.target.dataset.id-1;
    var updateStatusArr = this.data.fuelSel.slice(0);
    updateStatusArr[idx] = 1 - this.data.fuelSel[idx];
    this.setData({
      'fuelSel': updateStatusArr
    });
    console.log(this.data.fuelSel);
  },
  selectWeight: function(e) {
    let idx = e.target.dataset.id-1;
    var updateStatusArr = this.data.weightSel.slice(0);
    updateStatusArr[idx] = 1- this.data.weightSel[idx];
    this.setData({
      'weightSel': updateStatusArr
    });
    console.log(this.data.weightSel);
  },
  selectDragweight: function(e) {
    let idx = e.target.dataset.id-1;
    var updateStatusArr = this.data.dragweightSel.slice(0);
    updateStatusArr[idx] = 1 - this.data.dragweightSel[idx];
    this.setData({
      'dragweightSel': updateStatusArr
    });
    console.log(this.data.dragweightSel);
  },
  selecWeight: function (e) {
    let idx = e.target.dataset.id - 1;
    var updateStatusArr = this.data.weightSel.slice(0);
    updateStatusArr[idx] = 1 - this.data.weightSel[idx];
    this.setData({
      'weightSel': updateStatusArr
    });
    console.log(this.data.weightSel);
  },
  selectHorsepower: function (e) {
    let idx = e.target.dataset.id - 1;
    var updateStatusArr = this.data.horsepowerSel.slice(0);
    updateStatusArr[idx] = 1 - this.data.horsepowerSel[idx];
    this.setData({
      'horsepowerSel': updateStatusArr
    });
    console.log(this.data.horsepowerSel);
  },
  selectIshot: function (e) {
    let idx = e.target.dataset.id;
    var updateStatusArr = this.data.ishotSel.slice(0);
    if (updateStatusArr[idx] == 1) {
      updateStatusArr[idx] = 0;
    } else {
      updateStatusArr[idx] = 1;
      updateStatusArr[1-idx] = 0;
    }
    this.setData({
      'ishotSel': updateStatusArr
    });
    console.log(this.data.ishotSel);
  },

  confirmBtn: function() {
    var data = {};
    var paramstr = '';
    data.fuel = this.data.fuelSel;
    data.weight = this.data.weightSel;
    data.dragweight = this.data.dragweightSel;
    data.horsepower = this.data.horsepowerSel;
    data.ishot = this.data.ishotSel;
    var keynames = [];
    for(var i=0; i<data.fuel.length; i++) {
      if (data.fuel[i] == 1) {
        keynames.push({'key': this.data.fuelList[i].title});
      }
    }
    for(var i=0; i<data.weight.length; i++) {
      if (data.weight[i] == 1) {
        keynames.push({'key': this.data.weightList[i].title});
      }
    }
    for (var i = 0; i < data.dragweight.length; i++) {
      if (data.dragweight[i] == 1) {
        keynames.push({ 'key': this.data.dragweightList[i].title });
      }
    }
    for (var i = 0; i < data.horsepower.length; i++) {
      if (data.horsepower[i] == 1) {
        keynames.push({ 'key': this.data.horsepowerList[i].title });
      }
    }
    if (data.ishot[0] == 1) {
      keynames.push({ 'key': '促销'});
    } else if (data.ishot[1] == 1) {
      keynames.push({ 'key': '不促销' });
    }

    paramstr = '?type=filter&data=' + JSON.stringify(data) + '&key=' + JSON.stringify(keynames);
    //console.log(paramstr);
    wx.navigateTo({
      url: '../findCar/findCar' + paramstr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})